import router from './router/index'
import store from './store/index'
import { constantRoutes, asyncRoutes } from './router/index.js'

router.beforeEach(async (to, from, next) => {
    if (to.path === '/login') {
        next()
    } else {
        if (localStorage.getItem('TOKEN')) {
            if (store.state.userInfo.userId === undefined) {
                let {data: { data: { roles: { menus } } }} = await store.dispatch('updateUserInfo');
                let filterRoutes = asyncRoutes.filter(t => menus.includes(t.path.substr(1)));
                router.addRoutes(filterRoutes);
                router.options.routes = [...constantRoutes, ...filterRoutes];
            }
            next()
        } else {
            next('./login')
        }
    }
})